﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ACCPS1ProjectDemo.ControlDemo
{
    public partial class ListViewDemo : UserControl
    {
        public ListViewDemo()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 加载学生
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear(); //清空项
            string sql = "select top 9 * from student";
            DBHelper helper = new DBHelper();
            SqlDataReader reader = helper.ExecuteReader(sql);
            int imageIndex=0;
            while(reader.Read())
            {
                ListViewItem item = new ListViewItem(reader["StudentNo"].ToString());
                item.SubItems.Add(reader["StudentName"].ToString());
                item.SubItems.Add(reader["Phone"].ToString());
                item.SubItems.Add(reader["Address"].ToString());
                item.SubItems.Add(reader["GradeId"].ToString());
                item.SubItems.Add(reader["Birthday"].ToString());
                
                item.ImageIndex = imageIndex; //设置图片
                imageIndex++;

                listView1.Items.Add(item);
            }
            reader.Close();
        }
        /// <summary>
        /// 获得选中学生
        /// </summary>
        private void button2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
                MessageBox.Show("没有任何选中的学生！");
            else if (listView1.SelectedItems.Count > 1)
                MessageBox.Show("当前选中的学生数量为"+ listView1.SelectedItems.Count);
            else
            {
                string msg = "学号：" + listView1.SelectedItems[0].Text;
                msg += "\n姓名：" + listView1.SelectedItems[0].SubItems[1].Text;
                MessageBox.Show(msg);
            }
        }
        /// <summary>
        /// 视图切换
        /// </summary>
        private void button3_Click(object sender, EventArgs e)
        {
            if (listView1.View == View.LargeIcon)
                listView1.View = View.Details;  //详细信息视图
            else
                listView1.View = View.LargeIcon;  //大图标视图
        }

        /// <summary>
        /// 突显1年级学生
        /// </summary>
        private void button4_Click(object sender, EventArgs e)
        {
            //循环全部项
            foreach(ListViewItem item in listView1.Items)
            {
                if (item.SubItems[4].Text=="1")  //如果年级是1
                {
                    item.ForeColor = Color.Red; //改变文字颜色
                    item.BackColor = Color.Yellow; //改变背景颜色
                }
            }
        }

        /// <summary>
        /// 移除选中学生
        /// </summary>
        private void button5_Click(object sender, EventArgs e)
        {
            //仅仅是从ListView控件中移除，没有从数据库中删除
            if (listView1.SelectedItems.Count == 1)
            {
                listView1.SelectedItems[0].Remove(); //移除选中项
            }
            else
                MessageBox.Show("没有任何选中的学生！");
        }
        /// <summary>
        /// 按照年级分组
        /// </summary>
        private void button6_Click(object sender, EventArgs e)
        {
            DBHelper helper = new DBHelper();
            //创建分组
            string sql = "select * from grade";
            SqlDataReader reader = helper.ExecuteReader(sql);
            listView1.Groups.Clear();  //清除分组
            while (reader.Read())
            {
                //创建分组， 形式 ：ListViewGroup group = new ListViewGroup(分组名，分组显示名);
                ListViewGroup group = new ListViewGroup(reader["GradeId"].ToString(), reader["GradeName"].ToString());
                listView1.Groups.Add(group); //将分组添加到ListView控件中
            }
            reader.Close();
            //添加项
            listView1.Items.Clear(); //清空项
            sql = "select top 9 * from student";

            reader = helper.ExecuteReader(sql);
            int imageIndex = 0; //图片下标
             
            while (reader.Read())
            {
                ListViewItem item = new ListViewItem(reader["StudentNo"].ToString());
                item.SubItems.Add(reader["StudentName"].ToString());
                item.SubItems.Add(reader["Phone"].ToString());
                item.SubItems.Add(reader["Address"].ToString());
                item.SubItems.Add(reader["GradeId"].ToString());
                item.SubItems.Add(reader["Birthday"].ToString());

                item.ImageIndex = imageIndex; //设置图片
                imageIndex++;

                string gradeId = reader["GradeId"].ToString();
                listView1.Groups[gradeId].Items.Add(item); //将项添加到分组中
               
                listView1.Items.Add(item);//将项添加到ListView控件                
            }
            reader.Close();
        }
    }
}
